import Vue from 'vue'

//导入路由的包
import VueRouter from 'vue-router'
//安装路由
Vue.use(VueRouter)
// import './common/configHttp.js'

import VueResource from 'vue-resource'
Vue.use(VueResource)

import moment from 'moment'
import Vuex from 'vuex'
Vue.use(Vuex)

var car = JSON.parse(localStorage.getItem('car' || '[]'));
var store = new Vuex.Store({
  state:{
    car:car
  },
  mutations:{
   addToCar(state, goodsinfo){
     var flag = false;
     state.car.some(item => {
       if(item.id == goodsinfo.id){
         item.count += parseInt(goodsinfo.count)
         flag=true
         return true;
       }
     })
     console.log(flag);
     if(!flag){
       state.car.push(goodsinfo);
     }

     localStorage.setItem('car', JSON.stringify(state.car))

   },
   updateGoodsInfo(state, goodsinfo){
     state.car.forEach(item => {
       if(item.id == goodsinfo.id){
          item.count = parseInt(goodsinfo.count)
          return true
       }
     })
     localStorage.setItem('car', JSON.stringify(state.car))
   },
   removeFromCar(state, id){
     state.car.some((item,i) => {
       if(item.id == id){
         state.car.splice(i, 1)
         return true;
       }
     })
     localStorage.setItem('car', JSON.stringify(state.car))
   },

   updateGoodsSelected(state, info){
      state.car.some(item => {
        if(item.id == info.id){
           item.selected = info.selected
        }
      })

      localStorage.setItem('car', JSON.stringify(state.car))
   },
  },
  getters:{
    getAllCount(state){
      var c = 0;
      state.car.forEach(item => {
        c += item.count
      })
      return c;
    },
    getGoodsCount(state){
      var o = {}
      state.car.forEach(item => {
          o[item.id] = item.count
      })
      return o
    },
    getGoodsSelected(state){
      var o = {}
      state.car.forEach(item => {
        o[item.id] = item.selected
      })
      return o;
    },
    getGoodsCountAndAmount(state){
      var o = {
        count: 0,
        amount: 0
      }

      state.car.forEach(item => {
        if(item.selected){
          o.count += item.count
          o.amount += item.price * item.count
        }
      })
      return o
   }
  }
})

Vue.filter('dateFormat', function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss"){
   return moment(dataStr).format(pattern)
})
// Vue.http.options.root = 'http://vue.studyit.io';
// Vue.http.options.emulateJSON = true
// Vue.http.headers.common['Authorization'] = 'Basic YXBpOnBhc3N3b3Jk';

//导入MUI组件
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'


import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'

import preview from'vue-preview'
// Vue.use(VuePreview) 
Vue.use(preview, {
  mainClass: 'pswp--minimal--dark',
  barsSize: {top: 0, bottom: 0},
  captionEl: false,
  fullscreenEl: false,
  shareEl: false,
  bgOpacity: 0.85,
  tapToClose: true,
  tapToToggleControls: false
})

Vue.use(MintUI)
//按需导入 Mint-UI组件
// import {Header, Swipe, SwipeItem, Button, Lazyload} from 'mint-ui'
// Vue.component(Header.name, Header)
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);
Vue.config.productionTip = false
//导入自己router.js模块
import router from './router.js'
//导入 App根组件
import app from './App.vue'

var vm = new Vue({
  el: '#app',
  render: c => c(app),
  router,//挂在路由对象到vm实力上
  store
})